package tmp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestYouGou {
	public static List<String> listURL = new ArrayList<String>();
	public static Set<String> detailURLSet=new HashSet<String>();
	public static HashMap<String, String> seedMap=new HashMap<String, String>();
	
	public static void main(String[] args) {
		String seed1="http://www.yougou.com/search/7p~1~~~~~~~6~WOMEN_SHOES~~1";
		String seed2="http://www.yougou.com/search/w7~~1~~~~~~6~SPORTS_SHOES~~1";
		String seed3="http://www.yougou.com/search/Hi~1~~~~~~~6~MEN_SHOES~~1";
		String seed4="http://www.yougou.com/search/79~1~~~~~~~6~OUTDOOR~~1";
		String seed5="http://www.yougou.com/search/f6~1~~~~~~~6~CHILDREN_SHOES~~1";
		seedMap.put(seed1, null);
		seedMap.put(seed2, null);
		seedMap.put(seed3, null);
		seedMap.put(seed4, null);
		seedMap.put(seed5, null);
		getTotalPage(seedMap);
		getAllListURL(seedMap);
		getALLDetailURL(listURL);
		for (Iterator iterator = detailURLSet.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			System.out.println(name);
		}
		System.out.println(detailURLSet.size());
		
	}

	private static void getALLDetailURL(List<String> listurl) {
		for(int j=0;j<listurl.size();j++){
			String listURL=listurl.get(j);
			String content=Tool.Crawler.getContent(listURL);
			Pattern p=Pattern.compile("var json = '\\[(.*?)\\]");
			Matcher m=p.matcher(content);
			if(m.find()){
				content=m.group(1);
				String[] records=content.split("\\},\\{");
				for (int i = 0; i < records.length; i++) {
					String pno="";
					String cno="";
					p=Pattern.compile("\"commodityNo\":\"(.+?)\"");
					m=p.matcher(records[i]);
					if(m.find()){
						pno="/p_"+m.group(1)+".html";
					}
					p=Pattern.compile("\"threeLevelNo\":\"(.+?)\"");
					m=p.matcher(records[i]);
					if(m.find()){
						cno="/c_"+m.group(1);
					}
					detailURLSet.add("http://www.yougou.com"+cno+pno);
				}
			}
		}
		
	}

	private static void getAllListURL(HashMap<String, String> seedMap) {
		for (Iterator iterator = seedMap.keySet().iterator(); iterator.hasNext();) {
			String seed = (String) iterator.next();
			int pagenum=Integer.parseInt(seedMap.get(seed));
			String baseURL=seed.substring(0,seed.length()-1);
			for(int i=1;i<=pagenum;i++){
				listURL.add(baseURL+i);
			}
		}
	}

	private static void getTotalPage(HashMap<String,String> seedMap) {
		for (Iterator iterator = seedMap.keySet().iterator(); iterator.hasNext();) {
			String seed = (String) iterator.next();
			String content = Tool.Crawler.getContent(seed);
			Pattern p = Pattern
					.compile("var totalCount = freeMarkPaseInt\\('(.+?)'\\)");
			Matcher m = p.matcher(content);
			String totalCount = "0";
			if (m.find()) {
				totalCount  = m.group(1);
				totalCount  = totalCount .replaceAll(",", "");
			}
			int total=Integer.parseInt(totalCount);
			int pageNum=(total+40-1)/40;
			seedMap.put(seed, pageNum+"");
		}
	}
}
